package com.example.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Post;
import com.example.mapper.PostMapper;
import com.example.service.PostService;
import com.example.util.RedisUtil;
import com.example.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 子非鱼
 * @since 2020-07-20
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public IPage<PostVo> paging(Page page, Long categoryId, Long userId, Integer level, Boolean recommend, String order) {

        if (level == null) {
            level = -1;
        }

        QueryWrapper wrapper = new QueryWrapper<Post>()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(userId != null, "user_id", userId)
                .eq(level == 0, "level", 0)
                .gt(level > 0, "level", 0)
                .orderByDesc(order != null, order);

        return postMapper.selectPosts(page, wrapper);

    }

    @Override
    public PostVo selectOnePost(QueryWrapper<Post> wrapper) {
        return postMapper.selectOnePost(wrapper);
    }

    /**
     * 本周热议初始化
     */
    @Override
    public void initWeekRank() {

        // 获取7天内发表的文章
        List<Post> posts = this.list(new QueryWrapper<Post>()
                .ge("created", DateUtil.offsetDay(new Date(), -7))
                .select("id, title, user_id, comment_count, view_count, created")
        );
        System.out.println(posts);
        // 获取文章的总评论量
        for (Post post : posts) {
            String key = "day:rank:" + DateUtil.format(post.getCreated(), DatePattern.PURE_DATE_PATTERN);
            System.out.println(key);
            System.out.println(post);
            // ZADD [key] [post.id] [post.comment_count]
            redisUtil.zSet(key, post.getId(), post.getCommentCount());

            // 7天后自动过期()
            long between = DateUtil.between(new Date(), post.getCreated(), DateUnit.DAY);
            // 有效时间
            long expireTime = (7 - between) * 24 * 60 * 60;
            redisUtil.expire(key, expireTime);

            // 缓存文章的一些基本信息(id，标题，评论数量，作者)
            this.hashCachePostIdAndTitle(post, expireTime);
        }

        // 做并集(周评论最多排列)
        this.setWeekRank();

    }

    @Override
    public void incrCommentCountAndUnionForWeekRank(long postId, boolean isIncr) {
        String currentKey = "day:rank:" + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        redisUtil.zIncrementScore(currentKey, postId, isIncr ? 1 : -1);

        Post post = this.getById(postId);

        // 7天后自动过期(15号发表，7-(18-15)=4)
        long between = DateUtil.between(new Date(), post.getCreated(), DateUnit.DAY);
        // 有效时间
        long expireTime = (7 - between) * 24 * 60 * 60;

        // 缓存这篇文章的基本信息
        this.hashCachePostIdAndTitle(post, expireTime);

        // 重新做并集(周评论最多排列)
        this.setWeekRank();
    }

    /**
     * 本周合并评论数量操作
     */
    private void setWeekRank() {
        String currentKey = "day:rank:" + DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);

        String destKey = "week:rank";
        List<String> otherKeys = new ArrayList<>();
        for (int i = -6; i < 0; i++) {
            String temp = "day:rank:" + DateUtil.format(DateUtil.offsetDay(new Date(), i), DatePattern.PURE_DATE_PATTERN);
            otherKeys.add(temp);
        }
        // ZUNIONSTORE [key] [otherKeys] []
        redisUtil.zUnionAndStore(currentKey, otherKeys, destKey);
    }

    /**
     * 缓存文章的基本信息
     *
     * @param post       文章
     * @param expireTime 过期时间
     */
    private void hashCachePostIdAndTitle(Post post, long expireTime) {
        String key = "rank:post:" + post.getId();

        if (!redisUtil.hasKey(key)) {
            redisUtil.hset(key, "post:id", post.getId(), expireTime);
            redisUtil.hset(key, "post:title", post.getTitle(), expireTime);
            redisUtil.hset(key, "post:commentCount", post.getCommentCount(), expireTime);
        }
    }


}
